package com.qbccn.blog.controller.api;

import com.qbccn.blog.dto.ResponseCode;
import com.qbccn.blog.dto.ResponseResult;
import com.qbccn.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author linke
 * @date 2019-12-22 下午 21:45
 * @description
 */
@RestController
@RequestMapping("/api/user")
public class UserApi {

    @Autowired
    private UserService userService;

    @GetMapping("get")
    public String get() {
        return new Date() + "ok";
    }

    @PostMapping("p")
    public String p() {
        return new Date() + "ok~post";
    }

    @GetMapping("info")
    public ResponseResult info() {
        String username = getUsername();
        return new ResponseResult(ResponseCode.OK, userService.getByUsername(username));
    }

    private static String getUsername() {
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }
}
